package com.hjm.project.bankExpenditure.gbTradeInfo.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.utils.trade.LoFunction;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import com.hjm.project.bankExpenditure.gbTradeInfo.mapper.GbTradeInfoMapper;
import com.hjm.project.bankExpenditure.gbTradeInfo.domain.GbTradeInfo;
import com.hjm.project.bankExpenditure.gbTradeInfo.service.IGbTradeInfoService;
import com.hjm.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 钱宝官品交易信息Service业务层处理
 * 
 * @author maker-platform
 * @date 2022-10-17
 */
@Service
public class GbTradeInfoServiceImpl implements IGbTradeInfoService {
    @Resource
    private GbTradeInfoMapper gbTradeInfoMapper;

    /**
     * 查询钱宝官品交易信息
     * 
     * @param serial 钱宝官品交易信息ID
     * @return 钱宝官品交易信息
     */
    @Override
    public GbTradeInfo selectGbTradeInfoById(String serial) {
        return gbTradeInfoMapper.selectGbTradeInfoById(serial);
    }

    /**
     * 查询钱宝官品交易信息列表
     * 
     * @param gbTradeInfo 钱宝官品交易信息
     * @return 钱宝官品交易信息
     */
    @Override
    public List<GbTradeInfo> selectGbTradeInfoList(GbTradeInfo gbTradeInfo) {
        return gbTradeInfoMapper.selectGbTradeInfoList(gbTradeInfo);
    }

    @Override
    public Map selectGbTradeInfoListCount(GbTradeInfo gbTradeInfo) {
        return gbTradeInfoMapper.selectGbTradeInfoListCount(gbTradeInfo);
    }

    /**
     * 查询钱宝官品交易信息列表
     *
     * @param gbTradeInfo 钱宝官品交易信息
     * @return 钱宝官品交易信息 一个结果
     */
    @Override
    public GbTradeInfo selectGbTradeInfoOne(GbTradeInfo gbTradeInfo) {
        return gbTradeInfoMapper.selectGbTradeInfoOne(gbTradeInfo);
    }

    /**
     * 新增钱宝官品交易信息
     * 
     * @param gbTradeInfo 钱宝官品交易信息
     * @return 结果
     */
    @Override
    public int insertGbTradeInfo(GbTradeInfo gbTradeInfo) {
        return gbTradeInfoMapper.insertGbTradeInfo(gbTradeInfo);
    }

    /**
     * 修改钱宝官品交易信息
     * 
     * @param gbTradeInfo 钱宝官品交易信息
     * @return 结果
     */
    @Override
    public int updateGbTradeInfo(GbTradeInfo gbTradeInfo) {
        return gbTradeInfoMapper.updateGbTradeInfo(gbTradeInfo);
    }

    /**
     * 删除钱宝官品交易信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGbTradeInfoByIds(String ids) {
        return gbTradeInfoMapper.deleteGbTradeInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除钱宝官品交易信息信息
     * 
     * @param serial 钱宝官品交易信息ID
     * @return 结果
     */
    @Override
    public int deleteGbTradeInfoById(String serial) {
        return gbTradeInfoMapper.deleteGbTradeInfoById(serial);
    }


    /**
     * 推送交易分润
     * @param gbTradeInfo
     * @throws Exception
     */
    @Override
    public Map<String,String> sync(GbTradeInfo gbTradeInfo) throws Exception{
        Map<String, String> data = new HashMap<>();
        LoFunction lo = new LoFunction();
        data.put("merno", gbTradeInfo.getMerno());
        data.put("msgtype", gbTradeInfo.getMsgtype());
        data.put("serial", gbTradeInfo.getSerial());
        data.put("rrno", gbTradeInfo.getRrno());
        data.put("respcode", "EE");
        String reqdata= JSONObject.fromObject(data).toString();
        byte[] resByte = reqdata.getBytes();
        byte[] resp = connServer(resByte);
        String pack = lo.byte2HexStr(resp);
        String respStr = lo.hexStr2Str(pack);
        if ("200".equals(respStr)){
            data.put("code","0");
            data.put("msg","推送成功！");
            return data;
        }else{
            data.put("code","1");
            data.put("msg","推送失败！");
            return data;
        }
    }


    public byte[] connServer(byte[] reqPack) throws Exception {
        Socket socket = null;
        InputStream is = null;
        OutputStream os = null;
        LoFunction lo = new LoFunction();
        try {
            String ipAddress ="127.0.0.1";
            int port = 25506;
            socket = new Socket(); // 建立与服务器端的链接
            socket.connect(new InetSocketAddress(ipAddress, port), 60000); // 连接超时设置
            socket.setSoTimeout(60000); // 读写超时设置
            os = socket.getOutputStream();
            is = socket.getInputStream();
            os.write(reqPack);
            System.out.println("请求包：" + lo.byte2HexStr(reqPack));
            os.flush();
            // 接收服务器的响应
            byte[] lenByte = new byte[2];
            is.read(lenByte, 0, 2);
            int len = (int) lo.byteArrayToShort(lenByte);
            byte[] bb = new byte[len];
            is.read(bb, 0, len);
            byte[] respPack = new byte[lenByte.length + bb.length];
            System.arraycopy(lenByte, 0, respPack, 0, 2);
            System.arraycopy(bb, 0, respPack, lenByte.length, bb.length);
            return lo.checkByte(bb) ? null : bb;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            closeIO(socket, is, os);
        }
    }

    protected void closeIO(Socket socket, InputStream input, OutputStream output) {
        try {
            if (input != null) {
                input.close();
            }
            if (output != null) {
                output.close();
            }
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
        }
    }

}
